/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.service.customer;

import com.jeesite.common.service.TreeService;
import com.jeesite.modules.base.dao.customer.CustomerContactDao;
import com.jeesite.modules.base.dao.customer.CustomerDao;
import com.jeesite.modules.base.dao.customer.CustomerDataDao;
import com.jeesite.modules.base.dao.customer.CustomerShowroomDao;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.entity.customer.CustomerContact;
import com.jeesite.modules.base.entity.customer.CustomerData;
import com.jeesite.modules.base.entity.customer.CustomerShowroom;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户Service
 *
 * @author lv
 * @version 2021-02-25
 */
@Service
@Transactional(readOnly = true)
public class CustomerService extends TreeService < CustomerDao, Customer > {

    @Autowired
    private CustomerShowroomDao customerShowroomDao;
    @Autowired
    private CustomerContactDao customerContactDao;
    @Autowired
    private CustomerDataDao customerDataDao;
    @Autowired
    private CodeRuleService codeRuleService;

    /**
     * 获取单条数据
     *
     * @param customer
     * @return
     */
    @Override
    public Customer get(Customer customer) {
        Customer entity = super.get(customer);
        if (entity != null) {
            CustomerShowroom customerShowroom = new CustomerShowroom(entity);
            customerShowroom.setStatus(CustomerShowroom.STATUS_NORMAL);
            entity.setCustomerShowroomList(customerShowroomDao.findList(customerShowroom));
            CustomerContact customerContact = new CustomerContact(entity);
            customerContact.setStatus(CustomerContact.STATUS_NORMAL);
            entity.setCustomerContactList(customerContactDao.findList(customerContact));
            CustomerData customerData = new CustomerData(entity);
            customerData.setStatus(CustomerData.STATUS_NORMAL);
            entity.setCustomerDataList(customerDataDao.findList(customerData));
        }
        return entity;
    }

    /**
     * 查询列表数据
     *
     * @param customer
     * @return
     */
    @Override
    public List < Customer > findList(Customer customer) {
        return super.findList(customer);
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param customer
     */
    @Override
    @Transactional(readOnly = false)
    public void save(Customer customer) {
        if (customer.getIsNewRecord()) {
            customer.setCreateBy(UserUtils.getUser().getUserCode());
            customer.setUpdateBy(UserUtils.getUser().getUserCode());
            customer.setCustomerId(codeRuleService.getCode("customer"));
        }else {
            customer.setUpdateBy(UserUtils.getUser().getUserCode());
        }
        super.save(customer);
        // 保存 Customer子表
        for (CustomerShowroom customerShowroom : customer.getCustomerShowroomList()) {
            if (!CustomerShowroom.STATUS_DELETE.equals(customerShowroom.getStatus())) {
                customerShowroom.setCustomerId(customer);
                if (customerShowroom.getIsNewRecord()) {
                    customerShowroomDao.insert(customerShowroom);
                } else {
                    customerShowroomDao.update(customerShowroom);
                }
            } else {
                customerShowroomDao.delete(customerShowroom);
            }
        }
        // 保存 Customer子表
        for (CustomerContact customerContact : customer.getCustomerContactList()) {
            if (!CustomerContact.STATUS_DELETE.equals(customerContact.getStatus())) {
                customerContact.setCustomerId(customer);
                if (customerContact.getIsNewRecord()) {
                    customerContactDao.insert(customerContact);
                } else {
                    customerContactDao.update(customerContact);
                }
            } else {
                customerContactDao.delete(customerContact);
            }
        }
        // 保存 Customer子表
        int index = 0;
        for (CustomerData customerData : customer.getCustomerDataList()) {
            if (!CustomerData.STATUS_DELETE.equals(customerData.getStatus())) {
                customerData.setCustomerId(customer);
                if (customerData.getIsNewRecord()) {
                    customerDataDao.insert(customerData);
                } else {
                    customerDataDao.update(customerData);
                }
            } else {
                customerDataDao.delete(customerData);
            }
            // 保存上传附件
            FileUploadUtils.saveFileUpload(customerData.getId(),
                    "customerDataList[" + index + "].customerData_file");
            index++;
        }
        if (customer.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("customer");
        }
    }

    /**
     * 更新状态
     *
     * @param customer
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(Customer customer) {
        super.updateStatus(customer);
    }

    /**
     * 删除数据
     *
     * @param customer
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(Customer customer) {
        super.delete(customer);
        CustomerShowroom customerShowroom = new CustomerShowroom();
        customerShowroom.setCustomerId(customer);
        customerShowroomDao.deleteByEntity(customerShowroom);
        CustomerContact customerContact = new CustomerContact();
        customerContact.setCustomerId(customer);
        customerContactDao.deleteByEntity(customerContact);
        CustomerData customerData = new CustomerData();
        customerData.setCustomerId(customer);
        customerDataDao.deleteByEntity(customerData);
    }

    public Customer getByAbbreviation(Customer customer) {
        Customer where = new Customer();
        where.setCustomerNameAbbreviation(customer.getCustomerNameAbbreviation());
        return dao.getByEntity(where);
    }
}